package cn.com.ege.mvc.action.business;

import java.io.Serializable;
import java.util.List;

import javax.servlet.ServletConfig;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import cn.com.ege.mvc.controller.BaseAction;
import cn.com.ege.mvc.controller.IAction;
import cn.com.ege.mvc.exception.BusinessException;
import cn.com.ege.mvc.model.business.Supplier;
import cn.com.ege.mvc.service.impl.business.SupplierService;
import cn.com.ege.mvc.util.PageController;

public class SupplierAction extends BaseAction implements IAction, Serializable {
	private final static Log logger = LogFactory
			.getLog(MaterialTypeAction.class);
	private PageController pager = null;
	private Supplier supplier = null;
	private SupplierService service = null;
	private List<Supplier> supplierList = null;

	public String execute(HttpServletRequest request,
			HttpServletResponse response, ServletConfig servletConfig) {
		String act = request.getParameter("act");
		if (null != act) {
			if ("list".equals(act)) {
				// 取列表
				if ("list".equals(act)) {
					int totalRows = service.getRecordCount(new Supplier());
					String URL = request.getRequestURI();
					this.pager.setURL(URL);
					this.pager.setTotalRowsAmount(totalRows);
					supplierList = service
							.list(new Supplier(), this.pager.getPageStartRow(),
									pager.getPageSize(), "order by id desc", true);
					pager.setData(supplierList);
					return getReturnURL(LIST);
				}
			}
			// 点了添加或着点了修改
			else if ("get".equals(act)) {
				supplier = service.get(supplier);

				return getReturnURL(EDIT);
			}
			// 保存表单
			else if ("save".equals(act)) {
				try {
					service.save(supplier);
					return getReturnURL("getList");
				} catch (BusinessException e) {
					// 保存原来表单已输入的内容
					request.setAttribute("supplier", supplier);
					addMessage(e.getMessage());
					return getReturnURL(EDIT);
				}
			}
			// 删除
			else if ("del".equals(act)) {
				String[] ids = request.getParameterValues("id");
				for (String supplierId : ids) {
					Supplier sp = new Supplier();
					if (null != supplierId && !"0".equals(supplierId)) {
						sp.setId(Long.parseLong(supplierId));
						service.delete(sp);
					}
				}
				return getReturnURL("getList");
			}
		}
		return null;
	}

	public PageController getPager() {
		return pager;
	}

	public void setPager(PageController pager) {
		this.pager = pager;
	}

	public Supplier getSupplier() {
		return supplier;
	}

	public void setSupplier(Supplier supplier) {
		this.supplier = supplier;
	}

	public SupplierService getService() {
		return service;
	}

	public void setService(SupplierService service) {
		this.service = service;
	}

	public List<Supplier> getSupplierList() {
		return supplierList;
	}

	public void setSupplierList(List<Supplier> supplierList) {
		this.supplierList = supplierList;
	}

}
